package com.qianfeng.gl4study.youshare.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.qianfeng.gl4study.youshare.R;
import com.qianfeng.gl4study.youshare.model.Product;
import com.qianfeng.gl4study.youshare.utils.Constants;
import com.qianfeng.gl4study.youshare.utils.FileCache;
import com.qianfeng.gl4study.youshare.utils.ImageCache;
import com.qianfeng.gl4study.youshare.utils.ImageLoader;

import java.util.List;

/**
 * 商城核心产品列表显示的Adapter
 * Created with IntelliJ IDEA.
 * I'm glad to share my knowledge with you all.
 * User:Gaolei
 * Date:2015/2/6
 * Email:pdsfgl@live.com
 */
public class StoreCoreAdapter extends BaseAdapter {

	private List<Product> products;

	private LayoutInflater inflater;

	public StoreCoreAdapter(Context context,List<Product> products) {
		this.products = products;
		if(context!=null){
			inflater = LayoutInflater.from(context);
		}else {
			throw new IllegalArgumentException("Context must not null");
		}

	}

	@Override
	public int getCount() {
		int size = 0 ;
		if(products!=null) {
			size = products.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		Object ret = null;
		if(products!=null){
			ret = products.get(position);
		}
		return ret;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View ret = null;
		if(convertView!=null){
			ret = convertView;
		}else {
			ret = inflater.inflate(R.layout.item_store_core, parent, false);
		}


		ViewHolder holder = (ViewHolder) ret.getTag();
		if(holder==null) {
			holder = new ViewHolder();
			holder.textPosition = (TextView) ret.findViewById(R.id.txt_item_core_position);
			holder.textShortTitle = (TextView) ret.findViewById(R.id.txt_item_core_short_title);
			holder.textTitle = (TextView) ret.findViewById(R.id.txt_item_core_title);
			holder.imgIcon = (ImageView) ret.findViewById(R.id.ig_item_core_product_icon);
			holder.layout = (RelativeLayout) ret.findViewById(R.id.rl_store_core_bg);

		}
		switch (position%6){
			case 0:
				holder.layout.setBackgroundResource(R.drawable.store_core_item_16);
				break;
			case 1:
				holder.layout.setBackgroundResource(R.drawable.store_core_item_17);
				break;
			case 2:
				holder.layout.setBackgroundResource(R.drawable.store_core_item_18);
				break;
			case 3:
				holder.layout.setBackgroundResource(R.drawable.store_core_item_19);
				break;
			case 4:
				holder.layout.setBackgroundResource(R.drawable.store_core_item_20);
				break;
			case 5:
				holder.layout.setBackgroundResource(R.drawable.store_core_item_21);
				break;
		}
		Product product = products.get(position);
		String title = product.getGoodsName();

		String shortTitle = product.getAppGname();
		if(shortTitle==null||"".equals(shortTitle)){
			shortTitle = product.getGoodsName();
		}
		String txtPosition = (position + 1) + "";
		if(position<9){
			txtPosition= "0"+txtPosition;
		}
		holder.textPosition.setText(txtPosition);
		holder.textShortTitle.setText(shortTitle);
		holder.textTitle.setText(title);

		//加载默认图片,便面出现复用的View显示之前的图片
		holder.imgIcon.setImageResource(R.drawable.load_progress);


		/**
		 *开启异步任务
		 */
		//获取缩略图，采用相对路径，进行网址的处理
		String thumb = product.getGoodsThumb();

		if(thumb!=null&&thumb.length()>0) {
			if (!thumb.startsWith("http://") && !thumb.startsWith("https://")) {
				thumb = Constants.IMAGE_SERVICE + thumb;

			}

			//进行图片下载之前设置ImageView的tag，异步任务检查tag
			holder.imgIcon.setTag(thumb);

			//进行内存检查，如果有图片直接使用
			ImageCache imageCache = ImageCache.getInstance();
			Bitmap bitmap = imageCache.getImage(thumb);
			if(bitmap!=null){
				//包含图片对象，直接使用
				holder.imgIcon.setImageBitmap(bitmap);
			}else {
				//不包含缓存，进行本地文件的加载



				FileCache fileCache = FileCache.getInstance();
				byte[] bytes = fileCache.getContent(thumb);
				if(bytes!=null&&bytes.length>0){
					Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
					holder.imgIcon.setImageBitmap(bmp);
					imageCache.putImage(thumb,bmp);
				}else {
					ImageLoader imageLoader = new ImageLoader(holder.imgIcon);
					imageLoader.execute(thumb);
			}
			}
		}
		return ret;
	}

	private  static  class ViewHolder{
		public TextView textPosition;
		public TextView textShortTitle;
		public TextView textTitle;
		public ImageView imgIcon;
		public RelativeLayout layout;
	}


}
